package cn.sixmillions.ps.common.aspect;

import cn.sixmillions.ps.entity.MdPreview;
import cn.sixmillions.ps.service.IMdPreviewService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 * 密钥判断切面
 *
 * @author six
 */
@Aspect
@Component
@Slf4j
public class CheckSecretAspect {

    @Autowired
    private IMdPreviewService mdPreviewService;

    @Before("@annotation(cn.sixmillions.ps.common.aspect.CheckSecret)")
    public void after(JoinPoint joinPoint) {
        MdPreview input = (MdPreview) joinPoint.getArgs()[0];
        MdPreview mdPreview = mdPreviewService.getById(input.getMdId());
        Assert.notNull(mdPreview, "未查询到该文章");
        if (!input.getMdSecret().equals(mdPreview.getMdSecret())) {
            throw new RuntimeException("密钥校验错误");
        }
    }
}
